package com.lphoenix.pref_lab;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * 上海基分文化传播有限公司Shanghai Jifen Culture Communications Co., Ltd.
 * Copyright (c) 2018 Shanghai Jifen Culture Communications Co., Ltd. All rights reserved
 * <p>
 * Created By Liupeng
 * On 2021/11/30 4:17 下午
 * <p>
 * Desc
 */
//首先实现接口，重写方法
public class UnExceptionHandler implements Thread.UncaughtExceptionHandler {
    //静态，实例化一下当前的类
    private static UnExceptionHandler mUnCatchHandler = new UnExceptionHandler();
    //上下文
    private Context mContext;

    //写一个静态方法，返回当前类的实例
    public static UnExceptionHandler getInstance() {
        return mUnCatchHandler;
    }

    public void init(Context context) {
        //获取默认的系统异常捕获器
        //把当前的crash捕获器设置成默认的crash捕获器
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        try {
            //抽取成一个方法
            //            saveSD(e);

            long totalMemory = Runtime.getRuntime().totalMemory();
            long maxMemory = Runtime.getRuntime().maxMemory();
            long freeMemory = Runtime.getRuntime().freeMemory();
            Log.e("oom", "totalMemory..." + totalMemory + "..maxMemory.."
                    + maxMemory + "..freeMemory.." + freeMemory+"...vmSize..");
            String vmSize = DeviceUtil.getProcData();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //存储到sd卡
    private void saveSD(Throwable throwable) throws Exception {
        //首先判断SD卡的挂载状态
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.i("dj", "return");
            return;
        }

        //获取手机的一些信息
        PackageManager pm = mContext.getPackageManager();
        PackageInfo inFo = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);

        //获取版本信息
        String versionName = inFo.versionName;
        int versionCode = inFo.versionCode;

        int version_code = Build.VERSION.SDK_INT;

        //Android版本号
        String release = Build.VERSION.RELEASE;
        //手机型号
        String mobile = Build.MODEL;

        //手机制造商
        String mobileName = Build.MANUFACTURER;

        //获取SD卡的绝对路径
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        Log.i("dj", path);
        //获取时间
        Date date = new Date();
        String time = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", date);
        //创建一个文件夹
        File mDir = new File(path, "exception");
        mDir.mkdirs();
        //        创建一个文件
        File file = new File(mDir.getAbsolutePath(), "exception" + time + ".txt");
        //判断这是文件是否存在，不存在则创建
        if (!file.exists()) {
            file.createNewFile();
        }
        //拼接错误信息
        String data = "\nMobile型号：" + mobile + "\nMobileName：" + mobileName + "\nSDK版本：" + version_code +
                "\n版本名称：" + versionName + "\n版本号：" + versionCode + "\n异常信息：" + throwable.getMessage();

        Log.i("dj", data);
        //将字符串转换为字节
        byte[] buffer = data.trim().getBytes();
        //输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        // 开始写入数据到这个文件。
        fileOutputStream.write(buffer, 0, buffer.length);
        //刷新
        fileOutputStream.flush();
        //关流
        fileOutputStream.close();
    }
}